
echo $'\n'"=======  executing backup.sh [$apps]=============== "$'\n'

echo "---- back-up apps is $apps  ----"
echo "---- workDir is $workDir ----"
echo "---- bakDir is $bakDir ----"
echo "---- bakMaxDay is $bakMaxDay  ----"


if [  -z "$workDir" -o -z "$bakDir" -o -z "$apps" ]
then
    echo 'need variable :  [workDir] , [bakDir] and [apps]';
    exit 1;
fi

if [ ! -d "$bakDir" ]; then
    mkdir -pm 755 $bakDir
fi

if [ -z "$bakMaxDay" ]
then
    bakMaxDay=5
fi

for app in $apps
do
    moduleDir=$workDir/${app}
    if [ ! -d "$moduleDir" ]
    then
        mkdir -pm 755 $moduleDir && echo "---> $moduleDir created, and skip back-up"
        continue
    fi
    jar=`find $moduleDir -name "*.jar"`
    if [ -z "$jar" ]; then
        echo "---------> no jar found in [$moduleDir],skip back-up."
        continue;
    fi

    #删除老版本
    rm -rf `find $bakDir -mtime +$bakMaxDay -type d -name "$app*"`
    #当前日期
    date=`date +%Y%m%d%H%M`
    bakModuleDir=$bakDir/$app-$date

    echo "----==> moduleDir is ${moduleDir}"
    echo "----==> bakModuleDir is ${bakModuleDir}"
    mkdir -pm 755 ${bakModuleDir} && echo "----==> ${bakModuleDir} created"
    cp -rf $moduleDir/*  ${bakModuleDir} 2>/dev/null && echo "----==> $moduleDir made backup in ${bakModuleDir}"
    #清空部署目录
    rm -rf $moduleDir/*
done
echo "#########################  [$app] back-up finished #########################"


